/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName DBUpdator.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.dbmanager
{
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.utility.Application;
	
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;

	public class DBUpdator
	{
		public function DBUpdator()
		{
			
		}
		
		private function isDBExist():Boolean
		{
			
			var fileExist:Boolean = File.applicationStorageDirectory.resolvePath("iMultimax.db").exists;
			if(fileExist)
				return true;
			else
				return false;
			
		}
		
		private function getCurrentVersion():String
		{
			
			var dbVersion:String = null;
			
			if(isDBExist())
			{
				
				/*try
				{
					
					var iDBManager:IDatabaseManager = new DataBaseManager();
					dbVersion = iDBManager.getData("SELECT version FROM dbversion")[0].version;
					
				} 
				catch(error:DataBaseError) 
				{
					
					trace(error.message);
					throw error;
				}*/
				dbVersion = Application.appDBVersion;
			}
			else
			{
				
				dbVersion = Application.appDBVersion;
				
			}
				
			return dbVersion;
			
		}
		
		/***
		 * @Return number of versions missed
		 */ 
		private function getNumberOfVersionsMissed():Array
		{
			
			var numberOFVersionsMissed:Number;
			var previousVersion:String;
			var currentVersion:String = Application.appDBVersion;
			var arrVersion:ArrayCollection = new ArrayCollection();
			
			var fileExist:Boolean = File.applicationStorageDirectory.resolvePath("iMultimax.db").exists;
			if(fileExist)
			{
				var iDBManager:IDatabaseManager = new DataBaseManager();
				try
				{
					arrVersion = iDBManager.getData('SELECT v_version FROM IM_DATABASE_VERSION');
				} 
				catch(error:Error) 
				{
					trace("In DBUpdator:getNumberOfVersionsMissed()");
				}
				
				if(arrVersion.length > 0)
				{
					previousVersion = arrVersion[0].v_version;
				}
				else
				{
					previousVersion = "DB_VERSION_-1";
				}
			}
			else
			{
				previousVersion = "DB_VERSION_-1";
			}
			
			trace(currentVersion.substr(11, currentVersion.length-11));
			
			var previousVersionNumber:Number = Number( previousVersion.substr(11, previousVersion.length-11) );
			var currentVersionNumber:Number = Number( currentVersion.substr(11, currentVersion.length-11) );
			
			numberOFVersionsMissed = currentVersionNumber - previousVersionNumber;
			
			return [currentVersionNumber,previousVersionNumber];
			
		}
		
		public function updateDB():void
		{
			
			//var version:String = getCurrentVersion();
			var versionDetails:Array;
			var index:int = 0;
			var iDBManger:DataBaseManager;
			
			//index = int( version.substr(11, version.length-11) );
			
			//var arr:Array = DBConfig.DB_VERSIONS;
			
			//versionDetails = DBConfig.DB_VERSIONS[index];
			
			var currentPrevVersions:Array = getNumberOfVersionsMissed();
			
			var previousVersionNumber:Number = currentPrevVersions[1];
			var currentVersionNumber:Number =  currentPrevVersions[0];
			
			for(var i:Number=previousVersionNumber+1; i<=currentVersionNumber; i++)
			{
				
				versionDetails = DBConfig.DB_VERSIONS[i];
				iDBManger = new DataBaseManager();
				
				for(var j:int = 0; j<versionDetails.length; j++)
				{
					
					var query:String = versionDetails[j];
					
					try
					{
						
						iDBManger.execute(query);
						
					} 
					catch(error:DataBaseError) 
					{
						
						trace(error.message);
						
					}
					
				}
				versionDetails = null;
				
			}
			
			
			
			//Update db version table to know that it is executed once
			var iDataBaseManager:IDatabaseManager = new DataBaseManager();
			
			try
			{
				
				iDataBaseManager.execute("UPDATE IM_DATABASE_VERSION SET v_executed='true'");
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace('DBUpdator:update, update dbversion executed=true failed');
				
			}
			
			
		}
		
	}
	
}